home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / Class.java < prev    next >
Text File  |  1998-09-22  |  29KB  |  718 lines

  1. /*
  2.  * @(#)Class.java    1.57 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.lang;
  16. import java.lang.reflect.Member;
  17. import java.lang.reflect.Field;
  18. import java.lang.reflect.Method;
  19. import java.lang.reflect.Constructor;
  20. import java.io.InputStream;
  21.  
  22. /**
  23.  * Instances of the class </code>Class</code> represent classes and 
  24.  * interfaces in a running Java application.
  25.  * Every array also belongs to a class that is reflected as a Class
  26.  * object that is shared by all arrays with the same element type and
  27.  * number of dimensions.  Finally, the either primitive Java types
  28.  * (boolean, byte, char, short, int, long, float, and double) and
  29.  * the keyword void are also represented as Class objects.
  30.  * <p>
  31.  * There is no public constructor for the class </code>Class</code>. 
  32.  * </code>Class</code> objects are constructed automatically by the Java 
  33.  * Virtual Machine as classes are loaded and by calls to the 
  34.  * <code>defineClass</code> method in the class loader. 
  35.  * <p>
  36.  * The following example uses a Class object to print the Class name
  37.  * of an object:
  38.  * <p><pre><blockquote>
  39.  *     void printClassName(Object obj) {
  40.  *         System.out.println("The class of " + obj +
  41.  *                            " is " + obj.getClass().getName());
  42.  *     }
  43.  * </blockquote></pre>
  44.  *
  45.  * @author  unascribed
  46.  * @version 1.57, 07/01/98
  47.  * @see     java.lang.ClassLoader#defineClass(byte[], int, int)
  48.  * @since   JDK1.0
  49.  */
  50. public final
  51. class Class implements java.io.Serializable {
  52.     /*
  53.      * Constructor. Only the Java Virtual Machine creates Class
  54.      * objects.
  55.      */
  56.     private Class() {}
  57.  
  58.     /**
  59.      * Converts the object to a string. The string representation is the 
  60.      * string <code>"class"</code> or <code>"interface"</code> followed 
  61.      * by a space and then the fully qualified name of the class. 
  62.      * If this Class object represents a primitive type,
  63.      * returns the name of the primitive type.
  64.      *
  65.      * @return  a string representation of this class object. 
  66.      * @since   JDK1.0
  67.      */
  68.     public String toString() {
  69.     return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
  70.         + getName();
  71.     }
  72.  
  73.     /**
  74.      * Returns the <code>Class</code> object associated with the class 
  75.      * with the given string name. 
  76.      * Given the fully-qualified name for a class or interface, this
  77.      * method attempts to locate, load and link the class.  If it
  78.      * succeeds, returns the Class object representing the class.  If
  79.      * it fails, the method throws a ClassNotFoundException.
  80.      * <p>
  81.      * For example, the following code fragment returns the runtime 
  82.      * <code>Class</code> descriptor for the class named 
  83.      * <code>java.lang.Thread</code>: 
  84.      * <ul><code>
  85.      *   Class t = Class.forName("java.lang.Thread")
  86.      * </code></ul>
  87.      *
  88.      * @param      className   the fully qualified name of the desired class.
  89.      * @return     the <code>Class</code> descriptor for the class with the
  90.      *             specified name.
  91.      * @exception  ClassNotFoundException  if the class could not be found.
  92.      * @since      JDK1.0
  93.      */
  94.     public static native Class forName(String className)
  95.     throws ClassNotFoundException;
  96.  
  97.     /**
  98.      * Creates a new instance of a class. 
  99.      *
  100.      * @return     a newly allocated instance of the class represented by this
  101.      *             object. This is done exactly as if by a <code>new</code>
  102.      *             expression with an empty argument list.
  103.      * @exception  IllegalAccessException  if the class or initializer is
  104.      *               not accessible.
  105.      * @exception  InstantiationException  if an application tries to
  106.      *               instantiate an abstract class or an interface, or if the
  107.      *               instantiation fails for some other reason.
  108.      * @since     JDK1.0
  109.      */
  110.     public native Object newInstance() 
  111.     throws InstantiationException, IllegalAccessException;
  112.  
  113.     /**
  114.      * This method is the dynamic equivalent of the Java language
  115.      * <code>instanceof</code> operator. The method returns true if
  116.      * the specified Object argument is non-null and can be cast to
  117.      * the reference type represented by this Class object without
  118.      * raising a ClassCastException. It returns false otherwise.
  119.      *
  120.      * <p>Specifically, if this Class object represents a declared
  121.      * class, returns true if the specified Object argument is an
  122.      * instance of the represented class (or of any of its
  123.      * subclasses); false otherwise. If this Class object represents
  124.      * an array class, returns true if the specified Object argument
  125.      * can be converted to an object of the array type by an identity
  126.      * conversion or by a widening reference conversion; false
  127.      * otherwise. If this Class object represents an interface,
  128.      * returns true if the class or any superclass of the
  129.      * specified Object argument implements this interface; false
  130.      * otherwise. If this Class object represents a primitive type,
  131.      * returns false.
  132.      *
  133.      * @param   obj The object to check
  134.      * @since   JDK1.1
  135.      */
  136.     public native boolean isInstance(Object obj);
  137.  
  138.     /**
  139.      * Determines if the class or interface
  140.      * represented by this Class object is either the same as, or is a
  141.      * superclass or superinterface of, the class or interface
  142.      * represented by the specified Class parameter. It returns true
  143.      * if so, false otherwise. If this Class object represents a
  144.      * primitive type, returns true if the specified Class parameter
  145.      * is exactly this Class object, false otherwise.
  146.      *
  147.      * <p>Specifically, this method tests whether the type represented
  148.      * by the specified Class parameter can be converted to the type
  149.      * represented by this Class object via an identity conversion or
  150.      * via a widening reference conversion. See <em>The Java Language
  151.      * Specification</em>, sections 5.1.1 and 5.1.4 , for details.
  152.      *
  153.      * @exception NullPointerException if the specified Class parameter is null.
  154.      * @since   JDK1.1
  155.      */
  156.     public native boolean isAssignableFrom(Class cls);
  157.  
  158.     /**
  159.      * Determines if the specified Class object represents an interface type.
  160.      *
  161.      * @return  <code>true</code> if this object represents an interface;
  162.      *          <code>false</code> otherwise.
  163.      * @since   JDK1.0
  164.      */
  165.     public native boolean isInterface();
  166.  
  167.     /**
  168.      * If this Class object represents an array type, returns true,
  169.      * otherwise returns false.
  170.      *
  171.      * @since   JDK1.1
  172.      */
  173.     public native boolean isArray();
  174.  
  175.     /**
  176.      * Determines if the specified Class object represents a primitive Java
  177.      * type.
  178.      *
  179.      * <p>There are nine predefined Class objects to represent the eight
  180.      * primitive Java types and void.  These are created by the Java
  181.      * Virtual Machine, and have the same names as the primitive types
  182.      * that they represent, namely boolean, byte, char, short, int,
  183.      * long, float, and double, and void.
  184.      *
  185.      * <p>These objects may only be accessed via the following public
  186.      * static final variables, and are the only Class objects for
  187.      * which this method returns true.
  188.      *
  189.      * @see     java.lang.Boolean#TYPE
  190.      * @see     java.lang.Character#TYPE
  191.      * @see     java.lang.Byte#TYPE
  192.      * @see     java.lang.Short#TYPE
  193.      * @see     java.lang.Integer#TYPE
  194.      * @see     java.lang.Long#TYPE
  195.      * @see     java.lang.Float#TYPE
  196.      * @see     java.lang.Double#TYPE
  197.      * @see     java.lang.Void#TYPE
  198.      * @since   JDK1.1
  199.      */
  200.     public native boolean isPrimitive();
  201.  
  202.     /**
  203.      * Returns the fully-qualified name of the type (class, interface,
  204.      * array, or primitive) represented by this Class object, as a String.
  205.      *
  206.      * @return  the fully qualified name of the class or interface
  207.      *          represented by this object.
  208.      * @since   JDK1.0
  209.      */
  210.     public native String getName();
  211.  
  212.     /**
  213.      * Determines the class loader for the class. 
  214.      *
  215.      * @return  the class loader that created the class or interface
  216.      *          represented by this object, or <code>null</code> if the
  217.      *          class was not created by a class loader.
  218.      * @see     java.lang.ClassLoader
  219.      * @since   JDK1.0
  220.      */
  221.     public native ClassLoader getClassLoader();
  222.  
  223.     /**
  224.      * If this object represents any class other than the class 
  225.      * </code>Object</code>, then the object that represents the superclass 
  226.      * of that class is returned. 
  227.      * <p>
  228.      * If this object is the one that represents the class 
  229.      * </code>Object</code> or this object represents an interface, 
  230.      * </code>null</code> is returned. 
  231.      *
  232.      * @return  the superclass of the class represented by this object.
  233.      * @since   JDK1.0
  234.      */
  235.     public native Class getSuperclass();
  236.  
  237.     /**
  238.      * Determines the interfaces implemented by the class or interface 
  239.      * represented by this object. 
  240.      * <p>
  241.      * If this object represents a class, the return value is an array 
  242.      * containing objects representing all interfaces implemented by the 
  243.      * class. The order of the interface objects in the array corresponds 
  244.      * to the order of the interface names in the </code>implements</code> 
  245.      * clause of the declaration of the class represented by this object. 
  246.      * <p>
  247.      * If this object represents an interface, the array contains 
  248.      * objects representing all interfaces extended by the interface. The 
  249.      * order of the interface objects in the array corresponds to the 
  250.      * order of the interface names in the </code>extends</code> clause of 
  251.      * the declaration of the interface represented by this object. 
  252.      * <p>
  253.      * If the class or interface implements no interfaces, the method 
  254.      * returns an array of length 0. 
  255.      *
  256.      * @return  an array of interfaces implemented by this class.
  257.      * @since   JDK1.0
  258.      */
  259.     public native Class[] getInterfaces();
  260.  
  261.     /**
  262.      * If this class represents an array type, returns the Class
  263.      * object representing the component type of the array; otherwise
  264.      * returns null.
  265.      *
  266.      * @see     java.lang.reflect.Array
  267.      * @since   JDK1.1
  268.      */
  269.     public native Class getComponentType();
  270.  
  271.     /**
  272.      * Returns the Java language modifiers for this class or
  273.      * interface, encoded in an integer. The modifiers consist of the
  274.      * Java Virtual Machine's constants for public, protected,
  275.      * private, final, and interface; they should be decoded using the
  276.      * methods of class Modifier.
  277.      *
  278.      * <p>The modifier encodings are defined in <em>The Java Virtual
  279.      * Machine Specification</em>, table 4.1.
  280.      *
  281.      * @see     java.lang.reflect.Modifier
  282.      * @since   JDK1.1
  283.      */
  284.     public native int getModifiers();
  285.  
  286.     /**
  287.      * Get the signers of this class.
  288.      *
  289.      * @since   JDK1.1
  290.      */
  291.     public native Object[] getSigners();
  292.     
  293.     /**
  294.      * Set the signers of this class.
  295.      */
  296.     native void setSigners(Object[] signers);
  297.  
  298.     /**
  299.      * Not implemented in this version of the 
  300.      * Java<font size="-2"><sup>TM</sup></font> Development Kit. 
  301.      * <p>
  302.      * If the class or interface represented by this Class object is
  303.      * a member of another class, returns the Class object
  304.      * representing the class of which it is a member (its
  305.      * <em>declaring class</em>).  Returns null if this class or
  306.      * interface is not a member of any other class.
  307.      *
  308.      * @since   JDK1.1
  309.      */
  310.     public Class getDeclaringClass() {
  311.     return null;                /* not implemented */
  312.     }
  313.  
  314.     /**
  315.      * Not implemented in this version of the 
  316.      * Java<font size="-2"><sup>TM</sup></font> Development Kit. 
  317.      * <p>
  318.      * Returns an array containing Class objects representing all the
  319.      * public classes and interfaces that are members of the class
  320.      * represented by this Class object.  This includes public class
  321.      * and interface members inherited from superclasses and public
  322.      * class and interface members declared by the class.  Returns an
  323.      * array of length 0 if the class has no public member classes or
  324.      * interfaces, or if this Class object represents a primitive
  325.      * type.
  326.      *
  327.      * @since   JDK1.1
  328.      */
  329.     public Class[] getClasses() {
  330.     return new Class[0];            /* not implemented */
  331.     }
  332.  
  333.     /**
  334.      * Returns an array containing Field objects reflecting all the
  335.      * accessible public fields of the class or interface represented
  336.      * by this Class object.  Returns an array of length 0 if the
  337.      * class or interface has no accessible public fields, or if it
  338.      * represents an array type or a primitive type.
  339.      *
  340.      * <p>Specifically, if this Class object represents a class,
  341.      * returns the public fields of this class and of all its
  342.      * superclasses.  If this Class object represents an interface,
  343.      * returns the fields of this interface and of all its
  344.      * superinterfaces.  If this Class object represents an array type
  345.      * or a primitive type, returns an array of length 0.
  346.      *
  347.      * <p>The implicit length field for array types is not reflected
  348.      * by this method. User code should use the methods of class Array
  349.      * to manipulate arrays.
  350.      *
  351.      * <p>See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
  352.      *
  353.      * @exception SecurityException    if access to the information is denied.
  354.      * @see       java.lang.reflect.Field
  355.      * @since     JDK1.1
  356.      */
  357.     public Field[] getFields() throws SecurityException {
  358.     checkMemberAccess(Member.PUBLIC);
  359.     return getFields0(Member.PUBLIC);
  360.     }
  361.  
  362.     /**
  363.      * Returns an array containing Method objects reflecting all the
  364.      * public <em>member</em> methods of the class or interface
  365.      * represented by this Class object, including those declared by
  366.      * the class or interface and and those inherited from
  367.      * superclasses and superinterfaces. Returns an array of length 0
  368.      * if the class or interface has no public member methods.
  369.      *
  370.      * <p>See <em>The Java Language Specification</em>, sections 8.2
  371.      * and 8.4.
  372.      *
  373.      * @exception SecurityException    if access to the information is denied.
  374.      * @see       java.lang.reflect.Method
  375.      * @since     JDK1.1
  376.      */
  377.     public Method[] getMethods() throws SecurityException {
  378.     checkMemberAccess(Member.PUBLIC);
  379.     return getMethods0(Member.PUBLIC);
  380.     }
  381.  
  382.     /**
  383.      * Returns an array containing Constructor objects reflecting
  384.      * all the public constructors of the class represented by this
  385.      * Class object.  An array of length 0 is returned if the class
  386.      * has no public constructors.
  387.      *
  388.      * @exception SecurityException    if access to the information is denied.
  389.      * @see       java.lang.reflect.Constructor
  390.      * @since     JDK1.1
  391.      */
  392.     public Constructor[] getConstructors() throws SecurityException {
  393.     checkMemberAccess(Member.PUBLIC);
  394.     return getConstructors0(Member.PUBLIC);
  395.     }
  396.  
  397.     /**
  398.      * Returns a Field object that reflects the specified public
  399.      * member field of the class or interface represented by
  400.      * this Class object. The name parameter is a String specifying
  401.      * the simple name of the desired field.
  402.      *
  403.      * <p>The field to be reflected is located by searching all the
  404.      * member fields of the class or interface represented by this
  405.      * Class object for a public field with the specified name.
  406.      *
  407.      * <p>See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
  408.      *
  409.      * @exception NoSuchFieldException if a field with the specified name is
  410.      *              not found.
  411.      * @exception SecurityException    if access to the information is denied.
  412.      * @see       java.lang.reflect.Field
  413.      * @since     JDK1.1
  414.      */
  415.     public Field getField(String name)
  416.     throws NoSuchFieldException, SecurityException {
  417.     checkMemberAccess(Member.PUBLIC);
  418.     return getField0(name, Member.PUBLIC);
  419.     }
  420.  
  421.     /**
  422.      * Returns a Method object that reflects the specified public
  423.      * member method of the class or interface represented by this
  424.      * Class object. The name parameter is a String specifying the
  425.      * simple name the desired method, and the parameterTypes
  426.      * parameter is an array of Class objects that identify the
  427.      * method's formal parameter types, in declared order.
  428.      *
  429.      * <p>The method to reflect is located by searching all the member
  430.      * methods of the class or interface represented by this Class
  431.      * object for a public method with the specified name and exactly
  432.      * the same formal parameter types.
  433.      *
  434.      * <p>See <em>The Java Language Specification</em>, sections 8.2
  435.      * and 8.4.
  436.      *
  437.      * @exception NoSuchMethodException if a matching method is not found.
  438.      * @exception SecurityException    if access to the information is denied.
  439.      * @see       java.lang.reflect.Method
  440.      * @since     JDK1.1
  441.      */
  442.     public Method getMethod(String name, Class[] parameterTypes)
  443.     throws NoSuchMethodException, SecurityException {
  444.     checkMemberAccess(Member.PUBLIC);
  445.     return getMethod0(name, parameterTypes, Member.PUBLIC);
  446.     }
  447.  
  448.     /**
  449.      * Returns a Constructor object that reflects the specified public
  450.      * constructor of the class represented by this Class object. The
  451.      * parameterTypes parameter is an array of Class objects that
  452.      * identify the constructor's formal parameter types, in declared
  453.      * order.
  454.      *
  455.      * <p>The constructor to reflect is located by searching all the
  456.      * constructors of the class represented by this Class object for
  457.      * a public constructor with the exactly the same formal parameter
  458.      * types.
  459.      *
  460.      * @exception NoSuchMethodException if a matching method is not found.
  461.      * @exception SecurityException     if access to the information is denied.
  462.      * @see       java.lang.reflect.Constructor
  463.      * @since     JDK1.1
  464.      */
  465.     public Constructor getConstructor(Class[] parameterTypes)
  466.     throws NoSuchMethodException, SecurityException {
  467.     checkMemberAccess(Member.PUBLIC);
  468.     return getConstructor0(parameterTypes, Member.PUBLIC);
  469.     }
  470.  
  471.     /**
  472.      * Not implemented in this version of the 
  473.      * Java<font size="-2"><sup>TM</sup></font> Development Kit. 
  474.      * <p>
  475.      * Returns an array of Class objects reflecting all the classes
  476.      * and interfaces declared as members of the class represented by
  477.      * this Class object. This includes public, protected, default
  478.      * (package) access, and private classes and interfaces declared
  479.      * by the class, but excludes inherited classes and interfaces.
  480.      * Returns an array of length 0 if the class declares no classes
  481.      * or interfaces as members, or if this Class object represents a
  482.      * primitive type.
  483.      *
  484.      * @exception SecurityException    if access to the information is denied.
  485.      * @since     JDK1.1
  486.      */
  487.     public Class[] getDeclaredClasses() throws SecurityException {
  488.     checkMemberAccess(Member.DECLARED);
  489.     return new Class[0];            /* not implemented */
  490.     }
  491.  
  492.     /**
  493.      * Returns an array of Field objects reflecting all the fields
  494.      * declared by the class or interface represented by this Class
  495.      * object. This includes public, protected, default (package)
  496.      * access, and private fields, but excludes inherited
  497.      * fields. Returns an array of length 0 if the class or interface
  498.      * declares no fields, or if this Class object represents a
  499.      * primitive type.
  500.      *
  501.      * See <em>The Java Language Specification</em>, sections 8.2 and
  502.      * 8.3.
  503.      *
  504.      * @exception SecurityException    if access to the information is denied.
  505.      * @see       java.lang.reflect.Field
  506.      * @since     JDK1.1
  507.      */
  508.     public Field[] getDeclaredFields() throws SecurityException {
  509.     checkMemberAccess(Member.DECLARED);
  510.     return getFields0(Member.DECLARED);
  511.     }
  512.  
  513.     /**
  514.      * Returns an array of Method objects reflecting all the methods
  515.      * declared by the class or interface represented by this Class
  516.      * object. This includes public, protected, default (package)
  517.      * access, and private methods, but excludes inherited
  518.      * methods. Returns an array of length 0 if the class or interface
  519.      * declares no methods, or if this Class object represents a
  520.      * primitive type.
  521.      *
  522.      * <p>See <em>The Java Language Specification</em>, section 8.2.
  523.      *
  524.      * @exception SecurityException    if access to the information is denied.
  525.      * @see       java.lang.reflect.Method
  526.      * @since     JDK1.1
  527.      */
  528.     public Method[] getDeclaredMethods() throws SecurityException {
  529.     checkMemberAccess(Member.DECLARED);
  530.     return getMethods0(Member.DECLARED);
  531.     }
  532.  
  533.     /**
  534.      * Returns an array of Constructor objects reflecting all the
  535.      * constructors declared by the class represented by this Class
  536.      * object. These are public, protected, default (package) access,
  537.      * and private constructors.  Returns an array of length 0 if this
  538.      * Class object represents an interface or a primitive type.
  539.      *
  540.      * <p>See <em>The Java Language Specification</em>, section 8.2.
  541.      *
  542.      * @exception SecurityException    if access to the information is denied.
  543.      * @see       java.lang.reflect.Constructor
  544.      * @since     JDK1.1
  545.      */
  546.     public Constructor[] getDeclaredConstructors() throws SecurityException {
  547.     checkMemberAccess(Member.DECLARED);
  548.     return getConstructors0(Member.DECLARED);
  549.     }
  550.  
  551.     /**
  552.      * Returns a Field object that reflects the specified declared
  553.      * field of the class or interface represented by this Class
  554.      * object. The name parameter is a String that specifies the
  555.      * simple name of the desired field.
  556.      *
  557.      * @exception NoSuchFieldException if a field with the specified name is
  558.      *              not found.
  559.      * @exception SecurityException    if access to the information is denied.
  560.      * @see       java.lang.reflect.Field
  561.      * @since     JDK1.1
  562.      */
  563.     public Field getDeclaredField(String name)
  564.     throws NoSuchFieldException, SecurityException {
  565.     checkMemberAccess(Member.DECLARED);
  566.     return getField0(name, Member.DECLARED);
  567.     }
  568.  
  569.     /**
  570.      * Returns a Method object that reflects the specified declared
  571.      * method of the class or interface represented by this Class
  572.      * object. The name parameter is a String that specifies the
  573.      * simple name of the desired method, and the parameterTypes
  574.      * parameter is an array of Class objects that identify the
  575.      * method's formal parameter types, in declared order.
  576.      *
  577.      * @exception NoSuchMethodException if a matching method is not found.
  578.      * @exception SecurityException     if access to the information is denied.
  579.      * @see       java.lang.reflect.Method
  580.      * @since     JDK1.1
  581.      */
  582.     public Method getDeclaredMethod(String name, Class[] parameterTypes)
  583.     throws NoSuchMethodException, SecurityException {
  584.     checkMemberAccess(Member.DECLARED);
  585.     return getMethod0(name, parameterTypes, Member.DECLARED);
  586.     }
  587.  
  588.     /**
  589.      * Returns a Constructor object that reflects the specified declared
  590.      * constructor of the class or interface represented by this Class
  591.      * object.  The parameterTypes parameter is an array of Class
  592.      * objects that identify the constructor's formal parameter types,
  593.      * in declared order.
  594.      *
  595.      * @exception NoSuchMethodException if a matching method is not found.
  596.      * @exception SecurityException     if access to the information is denied.
  597.      * @see       java.lang.reflect.Constructor
  598.      * @since     JDK1.1
  599.      */
  600.     public Constructor getDeclaredConstructor(Class[] parameterTypes)
  601.     throws NoSuchMethodException, SecurityException {
  602.     checkMemberAccess(Member.DECLARED);
  603.     return getConstructor0(parameterTypes, Member.DECLARED);
  604.     }
  605.  
  606.     /**
  607.      * Finds a resource with a given name.  Will return null if no
  608.      * resource with this name is found.  The rules for searching a
  609.      * resources associated with a given class are implemented by the
  610.      * ClassLoader of the class.<p>
  611.      *
  612.      * The Class methods delegate to ClassLoader methods, after applying
  613.      * a naming convention: if the resource name starts with "/", it is used
  614.      * as is.  Otherwise, the name of the package is prepended, after
  615.      * converting "." to "/".
  616.      *
  617.      * @param   name the string representing the resource to be found
  618.      * @return  the <code>InputStream</code> object having the 
  619.      *             specified name, or <code>null</code> if no 
  620.      *             resource with the specified name is found.
  621.      * @see     java.lang.ClassLoader
  622.      * @see     java.lang.Class#getResource
  623.      * @since   JDK1.1
  624.      */
  625.     public InputStream getResourceAsStream(String name) {
  626.     name = resolveName(name);
  627.     ClassLoader cl = getClassLoader();
  628.     if (cl==null) {
  629.         // A system class.
  630.         return ClassLoader.getSystemResourceAsStream(name);
  631.     }
  632.     return cl.getResourceAsStream(name);
  633.     }
  634.  
  635.     /**
  636.      * Finds a resource with the specified name. The rules for searching 
  637.      * for resources associated with a given class are implemented by 
  638.      * the class loader of the class.
  639.      * <p>
  640.      * The Class methods delegate to ClassLoader methods, after applying
  641.      * a naming convention: if the resource name starts with "/", it is used
  642.      * as is.  Otherwise, the name of the package is prepended, after
  643.      * converting "." to "/".
  644.      *
  645.      * @param   name the string representing the resource to be found.
  646.      * @return  the <code>URL</code> object having the specified name,  
  647.      *             or <code>null</code> if no resource with the specified 
  648.      *             name is found.
  649.      * @see     java.lang.ClassLoader 
  650.      * @see     java.lang.Class#getResourceAsStream
  651.      * @since   JDK1.1
  652.      */
  653.     public java.net.URL getResource(String name) {
  654.     name = resolveName(name);
  655.     ClassLoader cl = getClassLoader();
  656.     if (cl==null) {
  657.         // A system class.
  658.         return ClassLoader.getSystemResource(name);
  659.     }
  660.     return cl.getResource(name);
  661.     }
  662.  
  663.     /*
  664.      * Return the Virtual Machine's Class object for the named
  665.      * primitive type.
  666.      */
  667.     static native Class getPrimitiveClass(String name);
  668.  
  669.     /*
  670.      * Check if client is allowed to access members.  If access is
  671.      * denied, throw a SecurityException.
  672.      *
  673.      * <p>Default policy: allow all clients access with normal Java
  674.      * access control.
  675.      */
  676.     private void checkMemberAccess(int which) {
  677.     SecurityManager s = System.getSecurityManager();
  678.     if (s != null) {
  679.         s.checkMemberAccess(this, which);
  680.     }
  681.     }
  682.  
  683.     /**
  684.      * Add a package name prefix if the name is not absolute
  685.      * Remove leading "/" if name is absolute
  686.      */
  687.     private String resolveName(String name) {
  688.     if (name == null) {
  689.         return name;
  690.     }
  691.     if (!name.startsWith("/")) {
  692.         Class c = this;
  693.         while (c.isArray()) {
  694.         c = c.getComponentType();
  695.         }
  696.         String baseName = c.getName();
  697.         int index = baseName.lastIndexOf('.');
  698.         if (index != -1) {
  699.         name = baseName.substring(0, index).replace('.', '/')
  700.             +"/"+name;
  701.         }
  702.     } else {
  703.         name = name.substring(1);
  704.     }
  705.     return name;
  706.     }
  707.  
  708.     private native Field[] getFields0(int which);
  709.     private native Method[] getMethods0(int which);
  710.     private native Constructor[] getConstructors0(int which);
  711.     private native Field getField0(String name, int which);
  712.     private native Method getMethod0(String name, Class[] parameterTypes,
  713.     int which);
  714.     private native Constructor getConstructor0(Class[] parameterTypes,
  715.     int which);
  716.  
  717. }
  718.